package com.ego.item.service.impl;

import com.ego.exception.enums.ExceptionEnum;
import com.ego.exception.exception.EgoException;
import com.ego.item.entity.Specification;
import com.ego.item.mapper.SpecificationMapper;
import com.ego.item.service.SpecificationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private SpecificationMapper specificationMapper;

    @Override
    @Transactional(readOnly = true, rollbackFor = RuntimeException.class)
    public Specification queryByCid(Long cid) {
        Specification specification = specificationMapper.selectById(cid);
        if (specification == null) {
            throw new EgoException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }
        return specification;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateSpecification(Specification specification) {
        int update = specificationMapper.updateById(specification);
        if (update < 0) {
            throw new EgoException(ExceptionEnum.UPDATE_SPEC_GROUP_FAILED);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void save(Specification specification) {
        int insert = specificationMapper.insert(specification);
        if (insert < 0){
            throw new EgoException(ExceptionEnum.SPEC_GROUP_CREATE_FAILED);
        }
    }


}
